
package com.yimixian.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.wheelview.CommentDialog;
import com.yimixian.app.util.SharedPreferencesHelper;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by songzy on 2015/10/29.
 */
public class OrderGoodsShowAdapter extends BaseAdapter {
    List<GoodsItem> mGoodsItems;
    Context mContext;
    private int mGoodId;

    @InjectView(R.id.goods_name_text)
    TextView mGoodsNameText;
    @InjectView(R.id.goods_quantity_text)
    TextView mGoodsQuantityText;
    @InjectView(R.id.goods_price_text)
    TextView mGoodsPriceText;
    @InjectView(R.id.tv_give)
    TextView mTvGive;
    @InjectView(R.id.tv_empty)
    TextView mTvEmpty;
    public OrderGoodsShowAdapter(Context context, List<GoodsItem> goodsItems) {
        this.mGoodsItems = goodsItems;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mGoodsItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mGoodsItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        View view = View.inflate(mContext, R.layout.orderdetail_goods_list_view, null);
        final TextView mCommentButton = (TextView) view.findViewById(R.id.order_comment_button);
        ButterKnife.inject(this, view);
        if (mGoodsItems != null && mGoodsItems.get(i) != null) {
            mGoodId = mGoodsItems.get(i).id;
            mGoodsNameText.setText(mGoodsItems.get(i).name);
            mGoodsQuantityText.setText(mGoodsItems.get(i).count);
            mGoodsPriceText.setText(mGoodsItems.get(i).price);
            SharedPreferencesHelper.saveRatingBarText1(mGoodsItems.get(i).tasteDesc);
            SharedPreferencesHelper.saveRatingBarText2(mGoodsItems.get(i).freshnessDesc);
            if (mGoodsItems.get(i).canComment) {
                mCommentButton.setText("评价水果");
                mCommentButton.setVisibility(View.VISIBLE);
            } else {
                mCommentButton.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(mGoodsItems.get(i).itemAttr)) {
                mTvGive.setVisibility(View.GONE);
            } else {
                mTvGive.setVisibility(View.VISIBLE);
                mTvGive.setText(mGoodsItems.get(i).itemAttr);
            }
            if (!TextUtils.isEmpty(mGoodsItems.get(i).textIndent)) {
                mTvEmpty.setText(mGoodsItems.get(i).textIndent);
            }  
        }
        mCommentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mContext != null) {
                    CommentDialog dialog = new CommentDialog(mContext);
                    if (mGoodsItems != null && mGoodsItems.get(i) != null) {
                        dialog.setGoodId(mGoodsItems.get(i).id);
                    } else {
                        dialog.setGoodId(0);
                    }
                    dialog.setButton(mCommentButton);
                    dialog.showDialog();
                }
            }
        });
        return view;
    }

     
}
